package com.henghai.zhenxuan.ui.page.mine;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.anythink.debug.api.ATDebuggerUITest;
import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.data.config.Const;
import com.henghai.zhenxuan.domain.event.Messages;
import com.henghai.zhenxuan.domain.message.PageMessenger;
import com.henghai.zhenxuan.domain.request.UnsubscribeRequester;
import com.henghai.zhenxuan.ui.page.login.AgreementActivity;
import com.henghai.zhenxuan.ui.page.login.LoginActivity;
import com.henghai.zhenxuan.ui.page.login.UpdatePwdActivity;
import com.henghai.zhenxuan.utils.Utils;
import com.henghai.zhenxuan.R;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

public class MineFragment extends BaseFragment {
    private MineStates mStates;
    private PageMessenger mMessenger;
    private UnsubscribeRequester mUnsubscribeRequester;


    private static final String TAG = "MineFragment";
    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MineStates.class);
        mMessenger = getApplicationScopeViewModel(PageMessenger.class);
        mUnsubscribeRequester = getFragmentScopeViewModel(UnsubscribeRequester.class);
        mUnsubscribeRequester.getTokenResult().observe(this, httpBeanDataResult -> {
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            AppConfig.INSTANCE.getMConfigs().token().set("");
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        });

    }

    private void iniViewData() {
        mStates.avatar.set(AppConfig.INSTANCE.getMConfigs().user().get().getAvatar());
        mStates.name.set(AppConfig.INSTANCE.getMConfigs().user().get().getNickName());
        mStates.phone.set("手机号:" + AppConfig.INSTANCE.getMConfigs().user().get().getPhonenumber());
        mStates.packageVersion.set("packageVersion:v" + AppConfig.INSTANCE.getMConfigs().login().get().getPackageVersion());
        mStates.idDebug.set(View.GONE);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");
        iniViewData();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        iniViewData();
        mMessenger.output(this, messages -> {
            if (messages.eventId == Messages.EVENT_USERINFO_SUCCESS) {
                iniViewData();
            }
        });
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_mine, BR.vm, mStates).addBindingParam(BR.click, new MineFragment.ClickProxy());
    }

    public class ClickProxy {

        public void address() {//我的地址
            nav().navigate(R.id.action_mainFragment_to_my_address);
        }

        public void detail() {//我的收货
            nav().navigate(R.id.action_mineFragment_to_MyHarvesFragment);
//            nav().navigate(R.id.action_mainFragment_to_IntegralDetailFragment);
        }

        public void myFastMail() {//我的快递
            nav().navigate(R.id.action_mineFragment_to_myFastMailFragment);
        }

        public void self() {//个人信息
            nav().navigate(R.id.action_mainFragment_to_SelfInfoFragment);
        }

        public void avatar() {
            Utils.showImage(getContext(), Const.URL_IMAGE + mStates.avatar.get().replace("/profile", ""));
        }

        public void team() {
//            nav().navigate(R.id.action_mainFragment_to_TeamFragment);

        }

        public void checkUpdate() {
            Utils.checkUpdate(getActivity());
        }

        public void rule() {
            nav().navigate(R.id.action_mainFragment_to_SignInRuleFragment);
        }


        public void updatePwd() {

            Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
            startActivity(intent);
        }

        public void invitationCode() {
            nav().navigate(R.id.action_mainFragment_to_InvitationCodeFragment);
        }

        public void logout() {
            MessageDialog.show("提示", "确认注销登录？").setOkButton("确认").setOkButton((dialog, v) -> {
                AppConfig.INSTANCE.getMConfigs().token().set("");
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
                return false;
            }).setCancelButton("取消");

        }

        public void agreement() {
            AgreementActivity.gotoAgreement("服务协议");
        }

        public void AdTest() {
            ATDebuggerUITest.showDebuggerUI(getContext(), "bdc6ab67635399e84c2ee8fe2f46bef5ff8268bc");
        }

        public void unsubscribe() {
            MessageDialog.show("注销账号", "注销后账号将无法登录，请确认账号中无形资产或虚拟权益将被清空，是否确认注销？").setOkButton("确认").setOkButton((dialog, v) -> {
                mUnsubscribeRequester.request(getViewLifecycleOwner(), "1");
                return false;
            }).setCancelButton("取消");
        }
    }

    public static class MineStates extends StateHolder {
        public final State<String> avatar = new State<>("");
        public final State<String> name = new State<>("");
        public final State<String> phone = new State<>("");

        public final State<Integer> appType = new State<>(AppConfig.INSTANCE.getMConfigs().login().get().getPackageVersion());
        public final State<String> packageVersion = new State<>("");
        public final State<Integer> idDebug = new State<>(Const.ISDEBUG ? View.VISIBLE : View.GONE);
    }

}
